﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;

using bdtheque.data.poco;
using bdtheque.data.interfaces;

namespace bdtheque.data.dataaccess
{
    public class BdthequeDataContext : DbContext
    {
        // Public Constructors
        public BdthequeDataContext(string connectionString) : base(connectionString) { }
        public BdthequeDataContext() : base() { } 

        // DbSet Definitions
        public DbSet<Author> AuthorDbSet { get; set; }
        public DbSet<Comic> ComicDbSet { get; set; }
        public DbSet<ComicSeries> ComicSeriesDbSet { get; set; }        
        public DbSet<Parameter> ParameterDbSet { get; set; }
        public DbSet<Profile> ProfileDbSet { get; set; }
        public DbSet<Publisher> PublisherDbSet { get; set; }
        public DbSet<User> UserDbSet { get; set; }
        public DbSet<UserCollection> UserCollectionDbSet { get; set; }

        // Model Restrictions
        protected override void OnModelCreating(DbModelBuilder dbModelBuilder)
        {
            // Author Restrictions
            dbModelBuilder.Entity<Author>().Property(a => a.Id).IsRequired();
            dbModelBuilder.Entity<Author>().Property(a => a.FirstName)
                .HasMaxLength(60)
                .IsRequired();
            dbModelBuilder.Entity<Author>().Property(a => a.LastName)
                .HasMaxLength(100)
                .IsRequired();

            // Comic Restrictions
            dbModelBuilder.Entity<Comic>().Property(c => c.Id).IsRequired();
            dbModelBuilder.Entity<Comic>().Property(c => c.Name)
                .HasMaxLength(200)
                .IsRequired();
            dbModelBuilder.Entity<Comic>().Ignore(c => c.Genre);
            dbModelBuilder.Entity<Comic>().Ignore(c => c.Language);   

            // ComicSeries Restrictions
            dbModelBuilder.Entity<ComicSeries>().Property(cs => cs.Id).IsRequired();
            dbModelBuilder.Entity<ComicSeries>().Property(cs => cs.Name)
                .HasMaxLength(200)
                .IsRequired();
            dbModelBuilder.Entity<ComicSeries>().Ignore(cs => cs.Genre);
            dbModelBuilder.Entity<ComicSeries>().Ignore(cs => cs.Language);   

            // Parameter Restrictions
            dbModelBuilder.Entity<Parameter>().Property(p => p.Id).IsRequired();
            dbModelBuilder.Entity<Parameter>().Property(p => p.Name)
                .HasMaxLength(100)
                .IsRequired();
            dbModelBuilder.Entity<Parameter>().Property(p => p.Value).IsRequired();

            // Profile Restrictions
            dbModelBuilder.Entity<Profile>().Property(pr => pr.Id).IsRequired();
            dbModelBuilder.Entity<Profile>().Property(pr => pr.Name)
                .HasMaxLength(40)
                .IsRequired();

            // Publisher Restrictions
            dbModelBuilder.Entity<Publisher>().Property(pu => pu.Id).IsRequired();
            dbModelBuilder.Entity<Publisher>().Property(pu => pu.Name)
                .HasMaxLength(60)
                .IsRequired();

            // User Restrictions
            dbModelBuilder.Entity<User>().Property(u => u.Id).IsRequired();
            dbModelBuilder.Entity<User>().Property(u => u.FirstName)
                .HasMaxLength(60)
                .IsRequired();            
            dbModelBuilder.Entity<User>().Property(u => u.LastName)
                .HasMaxLength(100)
                .IsRequired();
            dbModelBuilder.Entity<User>().Property(u => u.Email)
                .HasMaxLength(255)
                .IsRequired();

            dbModelBuilder.Entity<User>().Ignore(u => u.Role);               

            // UserCollection Restrictions
            dbModelBuilder.Entity<UserCollection>().Property(uc => uc.Id).IsRequired();
        }
    }
}
